from mako.template import Template
from mako.lookup import TemplateLookup
from mako.runtime import Context
# spezielles IO-Modul, um IO in/aus Strings auszufuehren
from StringIO import StringIO

#--------------------------------------------------------------------
#Diverse BasisKlassen und Hilffunktionen
#--------------------------------------------------------------------
class Page(object):
    title = 'Untitled Page'
    
    def header(self):
        return '''
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
         <style type="text/css">
         @import url('/css/style.css');
      </style>
</head>

<body>

<ul class="nav">
  <li class="nav"><a class="nav" href="/Lehrveranstaltungen">Lehrveranstaltungen</a></li>
  <li class="nav"><a class="nav" href="/Lerneinheiten">Lerneinheiten</a></li>
  <li class="nav"><a class="nav" href="/Marken">Marken</a></li>
  <li class="nav"><a class="nav" href="/Themen">Themen</a></li>
  <li class="nav"><a class="nav" href="/Fragen">Fragen</a></li>
</ul>
        ''' 
        #% (self.title, self.title)
    
    def footer(self):
        return '''
</body>
</html>
        '''
   
    def default(self, *arguments, **kwargs):
      msg_s = '''unbekannte Anforderung:
               %s
               %s''' % (str(arguments), str(kwargs))
      raise cherrypy.HTTPError(404, msg_s) 
    default.exposed= True

def isStrEmpty(strValue):
    if strValue == None or len(strValue) == 0:
        return True
    return False

def message(messageText):
    Lookup_o = TemplateLookup(directories=["."])
    # Template-Engine instanziieren, dabei Dateiname der Vorlage
    # angeben (hier fix codiert)
    # sowie Hinweis auf Ablageort weiterer (optionaler) Module
    Template_o = Template(filename="resources/Message.html",module_directory='.',lookup=Lookup_o)
    # Pufferbereich fuer IO erzeugen
    Buffer_o = StringIO()
    # Ausfuehrungskontext erzeugen, dabei den IO-Puffer zuordnen
    Context_o = Context(Buffer_o, messageAlert = messageText)
    # Vorlage mit dem aktuellen Kontext auswerten
    Template_o.render_context(Context_o)
    # Inhalt des IO-Puffers als String zurueckgeben
    return Buffer_o.getvalue()



#BEISPIEL SORTIEREN VON http://wiki.python.org/moin/HowTo/Sorting/
#>>> class Student:
#        def __init__(self, name, grade, age):
#                self.name = name
#                self.grade = grade
#                self.age = age
#        def __repr__(self):
#                return repr((self.name, self.grade, self.age))

#>>> student_objects = [
#        Student('john', 'A', 15),
#        Student('jane', 'B', 12),
#        Student('dave', 'B', 10),
#]
#>>> sorted(student_objects, key=lambda student: student.age)   # sort by age
#[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]